package com.my.study.algorithm.linked;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Stack;

/**
 * @author Carlos
 * @version 1.0
 * @Description
 * <p> 输入一个链表的头节点，按链表从尾到头的顺序返回每个节点的值（用数组返回）。
 * 0 <= 链表长度 <= 10000
 *
 * @date 2021/10/26 7:38
 **/
@Slf4j(topic = "c.ReverseLinkPrinted")
public class ReverseLinkPrinted {

    public static void main(String[] args) {
        ListNode node3 = new ListNode(3, null);
        ListNode node2 = new ListNode(2, node3);
        ListNode head = new ListNode(1, node2);

        log.debug("{}", head);

        ArrayList<Integer> tailToHead = printListFromTailToHead(head);

        System.out.println(tailToHead);

    }

    public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        if (null == listNode) {
            return new ArrayList<>();
        }

        Stack<Integer> stack = new Stack<>();

        ListNode temp = listNode;
        while (temp != null) {
            stack.push(temp.val);
            temp = temp.next;
        }

        ArrayList<Integer> result = new ArrayList<>();
        while (!stack.empty()) {
            result.add(stack.pop());
        }

        return result;
    }

}
